<?php
/*echo $_REQUEST['pro_id'];
echo $_REQUEST['project_name'];
echo $_REQUEST['project_manager'];
echo $_REQUEST['pro_description'];
echo $_REQUEST['start_date'];
echo $_REQUEST['complete_date'];
echo $_REQUEST['pro_status'];*/
include "../../operations/connection.php";
	function microtime_float()
	{
	    $a= (string)(microtime(true)*1000);
		$b=split('\.',$a);
		return $b[0];
	}
	
try {
	session_cache_expire(999999999);
	if(!isset($_SESSION)) 
{ 
session_start(); 
} 
	if (isset($_SESSION['addTester']) && isset($_SESSION['addCoder'])) {
		$testers = $_SESSION['addTester'];
		$coders = $_SESSION['addCoder'];
		$listComposition = $_SESSION['listcomposition'];		
			
		$projectid = $_REQUEST['projectid'];
		foreach ($testers as $tester) {
			$isExist = FALSE;
			foreach ($listComposition as $loadd){
	
				if ($loadd[2]==0 && $tester[1]==$loadd[1]) {
					$isExist = TRUE;
					if ($tester[6] != $loadd[6]) {
						mysql_query("UPDATE `pr_composition` SET `REmail`='".$tester[6]."' WHERE `IDComposition`=".$loadd[0]);
					}				
				}
			}
			if ($isExist == FALSE) {
				mysql_query("insert into pr_composition(IDComposition,IDProject,IDEmployee,Position, status,REmail) VALUES ('".microtime_float()."','".$projectid."','".$tester[1]."',0,0,".$tester[6].")");
			}
		}
		foreach ($coders as $coder) {
			$isExist = FALSE;
			foreach ($listComposition as $loadd){
				if ($loadd[2]==1 && ($loadd[1]==$coder[1])) {
					$isExist = TRUE;
					if ($coder[6] != $loadd[6]) {
						mysql_query("UPDATE `pr_composition` SET `REmail`='".$coder[6]."' WHERE `IDComposition`=".$loadd[0]);
					}
					
					
				}
			}
			if ($isExist == FALSE) {
				mysql_query("insert into pr_composition(IDComposition,IDProject,IDEmployee,Position, status,REmail) VALUES ('".microtime_float()."','".$projectid."','".$coder[1]."',1,0,".$coder[6].")");
			}
		}
		
		foreach ($listComposition as $loadd) {			
			if ($loadd[2]==0) {
				$isRemove = TRUE;
				foreach ($testers as $tester){					
					if ($tester[1]==$loadd[1]) {
						$isRemove = FALSE;
						break;	
					}	
				}
				if ($isRemove == TRUE) {
					mysql_query("delete from pr_composition WHERE IDComposition =".$loadd[0]);
				}
			}
			
		}
		foreach ($listComposition as $loadd) {
			
			if ($loadd[2]==1) {
				$isRemove = true;
				foreach ($coders as $coder){		
					if ($coder[1]==$loadd[1]) {				
						$isRemove = FALSE;
						break;	
					}	
				}
				if ($isRemove == true) {
					mysql_query("delete from pr_composition WHERE IDComposition =".$loadd[0]);
				}
			}
		}
		
		$_SESSION['addTester'] = null;
		$_SESSION['addCoder'] = null;
		$_SESSION['listcomposition'] = null;
		
	}
	
} catch (Exception $e) {
	mysql_close($connection);
}

mysql_close($connection);
if($_SESSION['role']==1)
	header("Location: ../../project_admin.php?content=admin/project/project-detail.php&id=".$_REQUEST['projectid']);
else 
	header("Location: ../../project.php?projectDetail=project-detail&id=".$_REQUEST['projectid']);


?>